package com.tools.common.object.json;

import cn.hutool.core.date.DateTime;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.tools.common.container.*;
import com.tools.common.container.r.*;
import com.tools.common.object.C;
import com.tools.common.object.Note;
import com.tools.common.object.json.deserializer.*;
import com.tools.common.object.json.deserializer.BasicTypeHandler;
import com.tools.db.ip.IPBean;
import com.tools.web.interceptor.auth.AuthUser;
import com.tools.web.interceptor.auth.GenericAuthToken;
import com.tools.web.interceptor.auth._common.MultipleAuthUser;
import com.tools.web.interceptor.auth._common.SingleAuthUser;

import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListSet;

/**
 * Jackson 的自定义反序列化器处理器工具类
 * */
@Note("Jackson 的自定义反序列化器处理器工具类")
public class DeserializerHandler {

    protected DeserializerHandler() {}

    @Note("若是混合类似的数组，那么转为 TreeSet 类型时")
    private static Comparator C;

    @Note("懒加载默认的 Comparator 比较器")
    public static Comparator getDefaultComparator() {
        if(C != null) return C;
        synchronized (DeserializerHandler.class) {
            if(C != null) return C;
            C = (o1, o2) -> 1;
            return C;
        }
    }

    /* *******************************************************************************************
     *
     *          返回所有的反序列化器
     *
     * *******************************************************************************************
     * */

    @Note("获取所有的 StdDeserializer 反序列化器")
    public static Map<Class, StdDeserializer> all() {
        Map<Class, StdDeserializer> map = new HashMap<>((int) (59/0.75) + 1);
        //类型包装类
        map.put(Class.class, TypeClassHandler.classDeserializer());
        map.put(C.class, TypeClassHandler.cStdDeserializer());
        //日期时间
        map.put(LocalDateTime.class, DateTimeHandler.localDateTimeDeserializer());
        map.put(LocalDate.class, DateTimeHandler.localDateDeserializer());
        map.put(LocalTime.class, DateTimeHandler.localTimeDeserializer());
        map.put(Date.class, DateTimeHandler.dateDeserializer());
        map.put(java.sql.Date.class, DateTimeHandler.sqlDateDeserializer());
        map.put(Timestamp.class, DateTimeHandler.timestampDeserializer());
        map.put(DateTime.class, DateTimeHandler.huToolDateTimeDeserializer());
        //自定义键值对容器
        StdDeserializer<OwnMap> ownMapStdDeserializer = KVTypeHandler.ownMapDeserializer();
        map.put(OwnPairs.class, ownMapStdDeserializer);
        map.put(AbsOwnPair.class, ownMapStdDeserializer);
        map.put(OwnMap.class, ownMapStdDeserializer);
        map.put(OwnPair.class, KVTypeHandler.ownPairDeserializer());
        map.put(JsonPair.class, KVTypeHandler.jsonPairDeserializer());
        //只读键值对容器
        map.put(OnlyReadHashMap.class, KVTypeHandler.onlyReadHashMapDeserializer());
        map.put(OnlyReadLinkedMap.class, KVTypeHandler.onlyReadLinkedMapDeserializer());
        map.put(OnlyReadTreeMap.class, KVTypeHandler.onlyReadTreeMapDeserializer());
        //Properties
        map.put(Properties.class, KVTypeHandler.propertiesDeserializer());
        //TreeMap 与 ConcurrentSkipListMap
        map.put(TreeMap.class, KVTypeHandler.treeMapDeserializer());
        map.put(ConcurrentHashMap.class, KVTypeHandler.concurrentSkipListMapDeserializer());
        //自定义集合容器
        StdDeserializer<OwnList> ownListStdDeserializer = RowTypeHandler.ownListDeserializer();
        map.put(OwnRows.class, ownListStdDeserializer);
        map.put(AbsOwnList.class, ownListStdDeserializer);
        map.put(OwnList.class, ownListStdDeserializer);
        StdDeserializer<OwnSet> ownSetStdDeserializer = RowTypeHandler.ownSetDeserializer();
        map.put(AbsOwnSet.class, ownSetStdDeserializer);
        map.put(OwnSet.class, ownSetStdDeserializer);
        //只读集合容器
        map.put(OnlyReadArrayList.class, RowTypeHandler.onlyReadArrayListDeserializer());
        map.put(OnlyReadHashSet.class, RowTypeHandler.onlyReadHashSetDeserializer());
        map.put(OnlyReadLinkedSet.class, RowTypeHandler.onlyReadLinkedSetDeserializer());
        map.put(OnlyReadTreeSet.class, RowTypeHandler.onlyReadTreeSetDeserializer());
        //TreeSet 与 ConcurrentSkipListSet
        map.put(TreeSet.class, RowTypeHandler.treeSetDeserializer());
        map.put(ConcurrentSkipListSet.class, RowTypeHandler.concurrentSkipListSetDeserializer());
        //AuthUser
        StdDeserializer<SingleAuthUser> singleAuthUserStdDeserializer = AuthUserTypeHandler.singleAuthUserDeserializer();
        map.put(SingleAuthUser.class, singleAuthUserStdDeserializer);
        map.put(AuthUser.class, singleAuthUserStdDeserializer);
        map.put(GenericAuthToken.class, singleAuthUserStdDeserializer);
        map.put(MultipleAuthUser.class, AuthUserTypeHandler.multipleAuthUserStdDeserializer());
        //IPBean 对象
        map.put(IPBean.class, IPBeanTypeHandler.ipBeanStdDeserializer());
        //基本类型
        map.putAll(BasicTypeHandler.all());
        return map;
    }

}
